package cn.jason.ymcc.mapper;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.SearchResultMapper;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class HighlightResultMapper implements SearchResultMapper {

    @Override
    public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> aClass, Pageable pageable) {
        //记录总条数
        long totalHits = response.getHits().getTotalHits();
        //记录列表
        List<T> list = Lists.newArrayList();
        // 获取搜索结果
        SearchHits hits = response.getHits();
        for (SearchHit hit : hits) {
            if(hits.getHits().length <= 0){
                return null;
            }
        // 将原本的JSON对象转换成Map对象
         Map<String, Object> map = hit.getSourceAsMap();
        // 获取高亮的字段Map
         Map<String, HighlightField> highlightFields = hit.getHighlightFields();
         for (Map.Entry<String, HighlightField> highlightField : highlightFields.entrySet()) {
        // 获取高亮的Key
         String key = highlightField.getKey();
        // 获取高亮的Value
        HighlightField value = highlightField.getValue();
        Text[] fragments = value.getFragments();
        StringBuilder sb = new StringBuilder();
          for (Text text : fragments) { sb.append(text);
             }map.put(key, sb.toString());
           }
        // 把Map转换成对象
        T item = JSON.parseObject(JSONObject.toJSONString(map),aClass);
            list.add(item);
        }
        // 返回的是带分页的结果
        return new AggregatedPageImpl<>(list, pageable, totalHits,response.getAggregations());
    }

    public <T> T mapSearchHit(SearchHit searchHit, Class<T> aClass) {
        return null;
    }
}